class openMaps {
	constructor(data){
		this.position =  data
	}
	open(type){
		const position = this.position
		const lng = position.lng; //经度39.9
		const lat = position.lat; //纬度116.4
		const address = position.address
		const title = position.title
		let timer = false
		
		uni.showLoading({
			title: '跳转中'
		});
		uni.getSystemInfo({
			success: (res) => {
				const openData = {
					map: '',
					href: '',
				}
				if (type == 0) {
					// openData.href = "https://uri.amap.com/marker?position=" + lng + "," + lat + "&name=" + position.address;
					openData.href = `https://uri.amap.com/marker?position=${lng},${lat}&name=${address}`
					// 高德地图
					if (res.platform == "android") {
						openData.map = `androidamap://viewMap?sourceApplication=appname&poiname=${address}&lat=${lat}&lon=${lng}&dev=0`
					} else {
						openData.map = `iosamap://viewMap?sourceApplication=appname&poiname=${address}&lat=${lat}&lon=${lng}&dev=0`
					}
				} else if (type == 1) {
					//由于打开需要1～2秒，利用这个时间差来处理－－打开app后，返回h5页面会出现页面变成app下载页面，影响用户体验
					openData.href = `http://api.map.baidu.com/marker?location=${lat},${lng}&title=${title}&content=${address}&output=html&src=webapp.baidu.openAPIdemo`;
					// 百度地图
					if (res.platform == "android") {
						openData.map = `bdapp://map/direction?region=${address}&origin=latlng:${lat},${lng}|name:${address}&destination=latlng:${lat},${lng}|name:${address}&coord_type=bd09ll&mode=driving&src=com.youbei.chefu`
					} else {
						// openData.map = `baidumap://map/marker?location=${lat},${lng}&title=${address}&content==${address}&src=andr.baidu.openAPIdemo&coord_type=gcj02`;
						openData.map = `baidumap://map/direction?region=${address}&origin=latlng:${lat},${lng}|name:${address}&destination=latlng:${lat},${lng}|name:${address}&coord_type=bd09ll&mode=driving&src=com.youbei.chefu`
					}
				} else {
					// 腾讯地图
					const qqmapkey = '' // 腾讯地图开发者key
					//调用腾讯地图
					openData.href = `https://apis.map.qq.com/uri/v1/routeplan?fromcoord=CurrentLocation&type=bus&to=${address}&tocoord=${lat},${lng}&policy=2&referer=yunjiaHUD`
					if (res.platform == "android") {
						openData.map = `qqmap://map/routeplan?coord=${lat},${lng}&title${address}&addr=${address}referer=${qqmapkey}`
					} else {
						openData.map = `qqmap://map/routeplan?coord=${lat},${lng}&title${address}&addr=${address}referer=${qqmapkey}`
					}
				}
				window.location.href = openData.map
				timer = true
				uni.hideLoading();
				//判断是否跳转
				setTimeout(function() {
					let hidden = window.document.hidden || window.document.mozHidden || window.document.msHidden || window.document.webkitHidden
					if (typeof hidden == "undefined" || hidden == false) {
						if(timer){
							timer = false
							//调用地图
							window.location.href = openData.href
						}
					}
				}, 2000);
			}
		})
	}
		
}

export default openMaps